package com.szholly.pro.watergas_server.common.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;

public class MySavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{  
    @Value(value = "${local.service.url}")  
    private String LOCAL_SERVER_URL;  
     
 
       private RequestCache requestCache = new HttpSessionRequestCache();  
 
       @Override  
       public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,  
               Authentication authentication) throws ServletException, IOException {  
    	   Object preUrl = request.getSession().getAttribute("preUrl");  
    	   System.out.println("【获取redirecturl】--->:"+preUrl);
           SavedRequest savedRequest = requestCache.getRequest(request, response); 
           
           if (savedRequest == null) {
               super.onAuthenticationSuccess(request, response, authentication);
               return;
           }
    
           this.requestCache.removeRequest(request, response);
           super.onAuthenticationSuccess(request, response, authentication);
       }  
 
       public void setRequestCache(RequestCache requestCache) {  
           this.requestCache = requestCache;  
       }  
}  
